package ge.telasi.tasks.fixture;

import ge.telasi.tasks.PersistenceUtils;
import ge.telasi.tasks.controller.TaskController;
import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.model.TaskReceiver;
import ge.telasi.tasks.model.User;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;

/**
 * Create tasks.
 *
 * @author dimitri
 */
public class TaskFixture extends Fixture<Task> {

    @Override
    protected Map<String, Task> execute(EntityManager em) {
        UserFixture uf = new UserFixture();
        User dimitri = uf.getEntity(em, UserFixture.DIMITRI);
        User nino = uf.getEntity(em, UserFixture.NINO);
        User gia = uf.getEntity(em, UserFixture.GIA);
        User shalva = uf.getEntity(em, UserFixture.SHALVA);
        List<User> assignees = new ArrayList<User>();
        assignees.add(nino);
        assignees.add(gia);
        assignees.add(shalva);
//        for (int i = 0; i < 179; i++) {
//            try {
//                createTask(em, "Subject " + (i + 1), "Body " + (i + 1),  dimitri, assignees);
//            } catch (Exception ex) {
//                ex.printStackTrace();
//            }
//        }
        return new HashMap<String, Task>();
    }

    void createTask(EntityManager em, String subject, String body,
            User owner, List<User> assignees) {
        Task task = new Task();
        task.setSubject(subject);
        task.setLastBody(body);
        task.setOwner(owner);
        task.setPriority(Task.PRIORITY_NORMAL);
        for (User user : assignees) {
            TaskReceiver receiver = new TaskReceiver();
            receiver.setCopy(false);
            receiver.setUser(user);
            task.getReceivers().add(receiver);
        }
        PersistenceUtils.beginTransaction(em);
        new TaskController().createTask(em, owner, task, null, false);
        PersistenceUtils.endTransaction(em);

    }

}
